﻿using BoriPeter.FatSecret.Models;
using BoriPeter.FitnessJournal8.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BoriPeter.FatSecret.Converters
{
    public static class FoodEntriesGetConverter
    {
        public static DailyEntry Convert(this FoodEntriesGetModel _inmodel, List<Food> _foods)
        {
            DailyEntry retModel = new DailyEntry();
            retModel.FoodEntries = new ObservableCollection<FoodEntry>();

            if (_inmodel.Entries != null)
            {
                foreach (var item in _inmodel.Entries)
                {
                    FoodEntry entry = new FoodEntry();
                    entry.Date = item.DateInt.ConvertToDate();
                    entry.EntryName = item.FoodEntryName;
                    entry.Id = item.FoodId;
                    entry.Meal = item.Meal;
                    entry.ServingId = item.ServingId;

                    Food actualFood;
                    if (_foods.Where(fo => fo.Id == item.FoodId).Count() > 0)
                    {
                        actualFood = _foods.Where(fo => fo.Id == item.FoodId).First();
                    }
                    else
                    {
                        actualFood = new Food();
                        actualFood.Name = item.FoodEntryName;
                        actualFood.Servings = new ObservableCollection<Serving>();

                        var newServing = new Serving();
                        newServing.Id = item.ServingId;
                        newServing.CalciumMg = item.Calcium;
                        newServing.CaloriesKcal = item.Calories;
                        newServing.CarbohydrateGram = item.Carbohydrate;
                        newServing.CholesterolMg = item.Cholesterol;
                        newServing.ProteinGram = item.Protein;
                        newServing.FatGram = item.Fat;
                        newServing.SaturatedFatGram = item.SaturatedFat;
                        newServing.PolyunsaturatedFatGram = item.PolyunsaturatedFat  ;
                        newServing.MonosaturatedFatGram = item.MonounsaturatedFat;
                        newServing.TransFatGram = item.TransFat;
                        newServing.CholesterolMg = item.Cholesterol;
                        newServing.SodiumMg = item.Sodium;
                        newServing.PotassiumMg = item.Potassium;
                        newServing.FiberGram = item.Fiber;
                        newServing.SugarGram = item.Sugar;
                        newServing.VitaminARDI = item.VitaminA;
                        newServing.VitaminCRDI = item.VitaminC;
                        newServing.CalciumRDI = item.Calcium;
                        newServing.IronRDI = item.Iron;

                        actualFood.Servings.Add(newServing);
                        _foods.Add(actualFood);
                    }

                    entry.Food = actualFood;

                    retModel.FoodEntries.Add(entry);
                }
            }

            return retModel;
        }
    }
}
